<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book python高级 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>股票收藏夹 | python高级</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../day18/05.review.html" />
    
    
    <link rel="prev" href="../day18/04.路由.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="8.5"
        data-chapter-title="股票收藏夹"
        data-filepath="day18/05.显示股票数据.md"
        data-basepath=".."
        data-revision="Wed Sep 26 2018 22:46:13 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="day11/00.readme.html">
            
                
                    <a href="../day11/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Python语法高级专题1
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="day11/01.GIL.html">
            
                
                    <a href="../day11/01.GIL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        GIL问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="day11/02.copy.html">
            
                
                    <a href="../day11/02.copy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        赋值-浅拷贝-深拷贝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="day11/03.import.html">
            
                
                    <a href="../day11/03.import.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        import导入模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="day11/04.review.html">
            
                
                    <a href="../day11/04.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="day12/00.readme.html">
            
                
                    <a href="../day12/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        Python语法高级专题2
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="day12/01.mro.html">
            
                
                    <a href="../day12/01.mro.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        多继承和MRO顺序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="day12/02.property.html">
            
                
                    <a href="../day12/02.property.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        property属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="day12/03.magic.html">
            
                
                    <a href="../day12/03.magic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        魔法属性和方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="day12/04.with.html">
            
                
                    <a href="../day12/04.with.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        with管理上下文
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="day12/05.review.html">
            
                
                    <a href="../day12/05.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="day13/00.readme.html">
            
                
                    <a href="../day13/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Python语法之装饰器
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="day13/01.closure.html">
            
                
                    <a href="../day13/01.closure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        闭包
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="day13/02.decorator.html">
            
                
                    <a href="../day13/02.decorator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        装饰器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="day13/03.review.html">
            
                
                    <a href="../day13/03.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="day14/00.readme.html">
            
                
                    <a href="../day14/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        MySQL数据库基本操作
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="day14/01.数据库概述.html">
            
                
                    <a href="../day14/01.数据库概述.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        数据库概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="day14/02.数据类型和约束.html">
            
                
                    <a href="../day14/02.数据类型和约束.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        数据完整性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="day14/03.Navicat.html">
            
                
                    <a href="../day14/03.Navicat.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        图形化客户端Navicat
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="day14/04.命令行客户端.html">
            
                
                    <a href="../day14/04.命令行客户端.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        命令行客户端SQL的使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.4.1" data-path="day14/05.sql_con.html">
            
                
                    <a href="../day14/05.sql_con.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.1.</b>
                        
                        登录和退出数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.2" data-path="day14/07.sql_table_struct.html">
            
                
                    <a href="../day14/07.sql_table_struct.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.2.</b>
                        
                        表结构的操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.3" data-path="day14/06.sql_database.html">
            
                
                    <a href="../day14/06.sql_database.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.3.</b>
                        
                        数据库的操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.4" data-path="day14/08.表数据操作.html">
            
                
                    <a href="../day14/08.表数据操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.4.</b>
                        
                        表数据的操作
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="day14/09.review.html">
            
                
                    <a href="../day14/09.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        知识总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="day15/00.readme.html">
            
                
                    <a href="../day15/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        MYSQL数据库进阶操作
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="day15/01.练习.html">
            
                
                    <a href="../day15/01.练习.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        练习强化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="day15/02.where条件.html">
            
                
                    <a href="../day15/02.where条件.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        条件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="day15/03.排序查询.html">
            
                
                    <a href="../day15/03.排序查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="day15/04.分页查询.html">
            
                
                    <a href="../day15/04.分页查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="day15/05.聚合函数.html">
            
                
                    <a href="../day15/05.聚合函数.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        聚合函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="day15/06.分组.html">
            
                
                    <a href="../day15/06.分组.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        分组
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="day15/07.连接.html">
            
                
                    <a href="../day15/07.连接.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="day15/08.自连接.html">
            
                
                    <a href="../day15/08.自连接.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        自连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.9" data-path="day15/09.子查询.html">
            
                
                    <a href="../day15/09.子查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                        子查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.10" data-path="day15/10.总结.html">
            
                
                    <a href="../day15/10.总结.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.10.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="day16/readme.html">
            
                
                    <a href="../day16/readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        MYSQL数据库编程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="day16/data.html">
            
                
                    <a href="../day16/data.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        数据准备
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="day16/sql_code.html">
            
                
                    <a href="../day16/sql_code.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        SQL演练
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="day16/dbdesign.html">
            
                
                    <a href="../day16/dbdesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        数据库设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="day16/pymysql.html">
            
                
                    <a href="../day16/pymysql.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        数据库编程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="day16/zongjie.html">
            
                
                    <a href="../day16/zongjie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="day17/00.readme.html">
            
                
                    <a href="../day17/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        MySQL数据库高级用法
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="day17/01.view.html">
            
                
                    <a href="../day17/01.view.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="day17/02.transaction.html">
            
                
                    <a href="../day17/02.transaction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        事务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="day17/03.sql_index.html">
            
                
                    <a href="../day17/03.sql_index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        索引
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="day17/04.user.html">
            
                
                    <a href="../day17/04.user.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        用户管理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="day17/05.master_slave.html">
            
                
                    <a href="../day17/05.master_slave.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        主从配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.6" data-path="day17/06.review.html">
            
                
                    <a href="../day17/06.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.6.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="day18/00.readme.html">
            
                
                    <a href="../day18/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        miniweb框架
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="day18/01.框架是什么.html">
            
                
                    <a href="../day18/01.框架是什么.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        框架概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="day18/02.实现框架.html">
            
                
                    <a href="../day18/02.实现框架.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        实现一个简单的框架
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="day18/03.模板.html">
            
                
                    <a href="../day18/03.模板.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        模板替换
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="day18/04.路由.html">
            
                
                    <a href="../day18/04.路由.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        路由概念
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="8.5" data-path="day18/05.显示股票数据.html">
            
                
                    <a href="../day18/05.显示股票数据.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        股票收藏夹
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="day18/05.review.html">
            
                
                    <a href="../day18/05.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        复习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >python高级</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="5-&#x80A1;&#x7968;&#x6536;&#x85CF;&#x5939;&#x6848;&#x4F8B;">5. &#x80A1;&#x7968;&#x6536;&#x85CF;&#x5939;&#x6848;&#x4F8B;</h1>
<h2 id="51-&#x51C6;&#x5907;&#x6848;&#x4F8B;&#x6240;&#x9700;&#x6570;&#x636E;">5.1 &#x51C6;&#x5907;&#x6848;&#x4F8B;&#x6240;&#x9700;&#x6570;&#x636E;</h2>
<h3 id="511-&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;">5.1.1 &#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;</h3>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">create</span> <span class="hljs-keyword">database</span> stock_db <span class="hljs-keyword">charset</span>=utf8;</span>
</code></pre>
<h3 id="512-&#x9009;&#x62E9;&#x6570;&#x636E;&#x5E93;">5.1.2. &#x9009;&#x62E9;&#x6570;&#x636E;&#x5E93;</h3>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">use</span> stock_db;</span>
</code></pre>
<h3 id="513-&#x5BFC;&#x5165;&#x6570;&#x636E;">5.1.3. &#x5BFC;&#x5165;&#x6570;&#x636E;</h3>
<p><code>stock_db.sql</code>&#x5728;&#x8BFE;&#x4EF6;&#x4E2D;</p>
<pre><code class="lang-sql">source stock_db.sql
</code></pre>
<h3 id="514-&#x8868;&#x7ED3;&#x6784;&#x5982;&#x4E0B;">5.1.4. &#x8868;&#x7ED3;&#x6784;&#x5982;&#x4E0B;</h3>
<pre><code class="lang-sql">mysql&gt; desc focus;
+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| note_info | varchar(200)     | YES  |     |         |                |
| info_id   | int(10) unsigned | YES  | MUL | NULL    |                |
+-----------+------------------+------+-----+---------+----------------+
</code></pre>
<pre><code class="lang-sql">mysql&gt; desc info;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| code     | varchar(6)       | NO   |     | NULL    |                |
| short    | varchar(10)      | NO   |     | NULL    |                |
| chg      | varchar(10)      | NO   |     | NULL    |                |
| turnover | varchar(255)     | NO   |     | NULL    |                |
| price    | decimal(10,2)    | NO   |     | NULL    |                |
| highs    | decimal(10,2)    | NO   |     | NULL    |                |
| time     | date             | YES  |     | NULL    |                |
+----------+------------------+------+-----+---------+----------------+
</code></pre>
<h2 id="52-&#x663E;&#x793A;&#x6240;&#x6709;&#x7684;&#x80A1;&#x7968;&#x4FE1;&#x606F;">5.2 &#x663E;&#x793A;&#x6240;&#x6709;&#x7684;&#x80A1;&#x7968;&#x4FE1;&#x606F;</h2>
<p>&#x521A;&#x521A;&#x521B;&#x5EFA;&#x7684;index&#x51FD;&#x6570;&#x6CA1;&#x80FD;&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;, &#x73B0;&#x5728;&#x7EC8;&#x4E8E;&#x53EF;&#x4EE5;&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#x5566;...</p>
<pre><code class="lang-py"><span class="hljs-decorator">@route(&apos;/index.html&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x5F53;&#x7528;&#x6237;&#x5728;&#x8BF7;&#x6C42;/index.html &#x7684;&#x65F6;&#x5019; &#x6267;&#x884C;&#x5F53;&#x524D;&#x51FD;&#x6570;&quot;&quot;&quot;</span>
    <span class="hljs-comment"># &#x8BFB;&#x53D6;&#x6A21;&#x677F;&#x6587;&#x4EF6;</span>
    <span class="hljs-keyword">with</span> open(<span class="hljs-string">&quot;template/index.html&quot;</span>, <span class="hljs-string">&apos;r&apos;</span>) <span class="hljs-keyword">as</span> file:
        html_data = file.read()

    <span class="hljs-comment"># TODO : &#x4ECE;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x67E5;&#x8BE2;&#x51FA;&#x6570;&#x636E;</span>
    data_from_mysql = <span class="hljs-string">&quot;&quot;</span>
    <span class="hljs-keyword">try</span>:
        conn = connect(host=<span class="hljs-string">&apos;localhost&apos;</span>, port=<span class="hljs-number">3306</span>, database=<span class="hljs-string">&apos;stock_db&apos;</span>, user=<span class="hljs-string">&apos;root&apos;</span>, password=<span class="hljs-string">&apos;mysql&apos;</span>, charset=<span class="hljs-string">&apos;utf8&apos;</span>)
        cursor = conn.cursor()
        sql = <span class="hljs-string">&quot;SELECT * FROM info;&quot;</span>
        cursor.execute(sql)
        <span class="hljs-comment"># &#x53D6;&#x51FA;&#x6240;&#x6709;&#x7684;&#x80A1;&#x7968;&#x4FE1;&#x606F;</span>
        <span class="hljs-comment"># &#xFF08;&#xFF08;&#xFF09;&#xFF08;&#xFF09;&#xFF08;&#xFF09;&#xFF09;(1, &apos;000007&apos;, &apos;&#x5168;&#x65B0;&#x597D;&apos;, &apos;10.01%&apos;, &apos;4.40%&apos;, Decimal(&apos;16.05&apos;), Decimal(&apos;14.60&apos;), datetime.date(2017, 7, 18))</span>
        <span class="hljs-comment"># data_from_mysql = str(cursor.fetchall())</span>
        <span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> cursor.fetchall():

            data_line = <span class="hljs-string">&quot;&quot;&quot;&lt;tr&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;&lt;input type=&quot;button&quot; value=&quot;&#x6DFB;&#x52A0;&quot; id=&quot;toAdd&quot; name=&quot;toAdd&quot; systemidvaule=&quot;%s&quot;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &quot;&quot;&quot;</span> % (line[<span class="hljs-number">0</span>],line[<span class="hljs-number">1</span>],line[<span class="hljs-number">2</span>],line[<span class="hljs-number">3</span>],line[<span class="hljs-number">4</span>],line[<span class="hljs-number">5</span>],line[<span class="hljs-number">6</span>],line[<span class="hljs-number">7</span>],line[<span class="hljs-number">1</span>])
            data_from_mysql += data_line
    <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;ERROR&quot;</span> + str(e)
    <span class="hljs-keyword">else</span>:
        <span class="hljs-comment"># &#x8BFB;&#x53D6;&#x6570;&#x636E; &#x653E;&#x5230;&#x6A21;&#x677F;&#x6587;&#x4EF6; &#x62FC;&#x63A5;&#x6210;&#x7528;&#x6237;&#x9700;&#x8981;&#x7684;&#x7F51;&#x9875; -- &#x6A21;&#x677F;&#x66FF;&#x6362;</span>
        <span class="hljs-comment"># &#x5C06;&#x6A21;&#x677F;&#x6587;&#x4EF6;&#x4E2D;&#x6A21;&#x677F;&#x53D8;&#x91CF;{%content%}&#x66FF;&#x6362;&#x4E3A;&#x7528;&#x6237;&#x9700;&#x8981;&#x7684;&#x6570;&#x636E;</span>
        html_data = html_data.replace(<span class="hljs-string">&quot;{%content%}&quot;</span>, data_from_mysql)
        <span class="hljs-keyword">return</span> html_data

    <span class="hljs-keyword">finally</span>:
        <span class="hljs-keyword">if</span> <span class="hljs-string">&quot;cursor&quot;</span> <span class="hljs-keyword">in</span> locals():
            cursor.close()
        <span class="hljs-keyword">if</span> <span class="hljs-string">&quot;conn&quot;</span> <span class="hljs-keyword">in</span> locals():
            conn.close()
</code></pre>
<p><img src="../assets/day18/Xnip2018-07-04_21-31-09.png" alt=""></p>
<h2 id="53-&#x663E;&#x793A;&#x6240;&#x6709;&#x5173;&#x6CE8;&#x7684;&#x80A1;&#x7968;&#x4FE1;&#x606F;">5.3 &#x663E;&#x793A;&#x6240;&#x6709;&#x5173;&#x6CE8;&#x7684;&#x80A1;&#x7968;&#x4FE1;&#x606F;</h2>
<p>&#x9700;&#x8981;&#x5728;&#x6846;&#x67B6;&#x4E2D;&#x65B0;&#x589E;&#x4E00;&#x4E2A;center&#x51FD;&#x6570; &#x5B9E;&#x73B0;/center.html&#x9875;&#x9762;&#x7684;&#x521B;&#x5EFA;</p>
<pre><code class="lang-py"><span class="hljs-decorator">@route(&quot;/center.html&quot;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">center</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x8FD4;&#x56DE;center.html&#x9700;&#x8981;&#x7684;&#x9875;&#x9762;&#x5185;&#x5BB9;&quot;&quot;&quot;</span>
    <span class="hljs-comment"># return &quot;hahha&quot; + os.getcwd()  # for test &#x8DEF;&#x5F84;&#x95EE;&#x9898;</span>
    <span class="hljs-keyword">try</span>:
        file_name = file_name.replace(<span class="hljs-string">&quot;.py&quot;</span>, <span class="hljs-string">&quot;.html&quot;</span>)
        f = open(template_root + file_name)
    <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> ret:
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;%s&quot;</span> % ret
    <span class="hljs-keyword">else</span>:
        content = f.read()
        f.close()

        db = pymysql.connect(host=<span class="hljs-string">&apos;localhost&apos;</span>,port=<span class="hljs-number">3306</span>,user=<span class="hljs-string">&apos;root&apos;</span>,password=<span class="hljs-string">&apos;mysql&apos;</span>,database=<span class="hljs-string">&apos;stock_db&apos;</span>,charset=<span class="hljs-string">&apos;utf8&apos;</span>)
        cursor = db.cursor()
        sql = <span class="hljs-string">&quot;&quot;&quot;select i.code,i.short,i.chg,i.turnover,i.price,i.highs,j.note_info from info as i inner join focus as j on i.id=j.info_id;&quot;&quot;&quot;</span>
        cursor.execute(sql)
        data_from_mysql = cursor.fetchall()
        cursor.close()
        db.close()

        html_template = <span class="hljs-string">&quot;&quot;&quot;
            &lt;tr&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;%s&lt;/td&gt;
                &lt;td&gt;
                    &lt;a type=&quot;button&quot; class=&quot;btn btn-default btn-xs&quot; href=&quot;/update/%s.html&quot;&gt; &lt;span class=&quot;glyphicon glyphicon-star&quot; aria-hidden=&quot;true&quot;&gt;&lt;/span&gt; &#x4FEE;&#x6539; &lt;/a&gt;
                &lt;/td&gt;
                &lt;td&gt;
                    &lt;input type=&quot;button&quot; value=&quot;&#x5220;&#x9664;&quot; id=&quot;toDel&quot; name=&quot;toDel&quot; systemidvaule=&quot;%s&quot;&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            &quot;&quot;&quot;</span>
        html = <span class="hljs-string">&quot;&quot;</span>
        <span class="hljs-keyword">for</span> info <span class="hljs-keyword">in</span> data_from_mysql:
            html += html_template % (info[<span class="hljs-number">0</span>], info[<span class="hljs-number">1</span>], info[<span class="hljs-number">2</span>], info[<span class="hljs-number">3</span>], info[<span class="hljs-number">4</span>], info[<span class="hljs-number">5</span>], info[<span class="hljs-number">6</span>], info[<span class="hljs-number">0</span>], info[<span class="hljs-number">0</span>])

        content = re.sub(<span class="hljs-string">r&quot;\{%content%\}&quot;</span>, html, content)
        <span class="hljs-keyword">return</span> content
</code></pre>
<p><img src="../assets/day18/Xnip2018-07-04_21-31-54.png" alt=""></p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../day18/04.路由.html" class="navigation navigation-prev " aria-label="Previous page: 路由概念"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../day18/05.review.html" class="navigation navigation-next " aria-label="Next page: 复习"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book python高级 -->
</html>
